<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2022/3/16
 * Time: 3:59 下午
 */

namespace app\common\cachemodel\zlcr2;

use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

/**
 * Class GuessQuestion
 * @package app\common\cachemodel\zlcr2
 */
class GuessQuestion extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr2_guess_question';
    protected $cacheModel = 'question_answer.guess_question';

    /**
     * 批量获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $model = $this->where(['g.id' => ['in', $ids]]);
            } else
            {
                $model = $this->where(['g.status' => 1, 'g.type' => $type[0]]);
            }
            $data = $model->alias('g')->join('wechat_backup_07_18_14_43_00.zlcr2_guess_question_img gi', 'g.img_id=gi.id')->column('gi.img,gi.color,g.*', 'g.id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    /**
     * 更新数据
     * @param $qid
     * @param $data
     * @return bool
     */
    public function UpdateData($qid, $data)
    {
        try
        {
            $this->where(['id' => $qid])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $qid)->setCache($data);

            return true;
        }
        catch (\Exception $e)
        {
            Log::write('问答 GuessQuestion model => UpdateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }

    /**
     * 获取猜你想问列表
     * @param $type 0=>所有 1=>问答 2=>日运
     * @return array
     * @throws \Exception
     */
    public function getDataList(int $type = 1)
    {
        $dataList = $this->getListPage($type, 'sort', ['id', 'question', 'same_num', 'same_base', 'color', 'img']);

        $resData = [];
        foreach ($dataList['data'] as $item)
        {
            $resData[] = [
                'text' => $item['question'],
                'tip' => ($item['same_num'] + $item['same_base']) . '人已问',
                'color' => $item['color'],
                'img' => $item['img'],
                'target' => 'local',
                'link' => "question_recomend?qid={$item['id']}&need_auth=0",
                'type' => 1, //  1=>猜你先问 2=>banner
            ];
        }

        return $resData;
    }
}
